package org.saxion.concurrency;

import java.util.ArrayList;
import java.util.Random;

public class Autorai extends Thread {

	private ShowRoom showRoom;
	int counter = 0;

	public Autorai() {
		showRoom = new ShowRoom();
		Bouncer bouncer = new Bouncer("Bouncer", showRoom);
		bouncer.start();

	}

	public void run() {
		testRunViewers();
	}

	public void testRunBuyers() {
		ArrayList<Person> personen = new ArrayList<Person>();
		personen.add(new Buyer("b1", showRoom));
		personen.add(new Buyer("b2", showRoom));
		personen.add(new Buyer("b3", showRoom));
		personen.add(new Viewer("v1", showRoom));
		for (Person p : personen) {
			waitSomeTime();
			p.start();
		}
	}

	public void testRunViewers() {
		ArrayList<Person> personen = new ArrayList<Person>();
		personen.add(new Viewer("v1", showRoom));
		personen.add(new Viewer("v2", showRoom));
		personen.add(new Viewer("v3", showRoom));
		personen.add(new Buyer("b1", showRoom));
		for (Person p : personen) {
//			waitSomeTime();
			p.start();
		}
	}

	public void generatePeople() {
		while (true) {
			while (showRoom.getQueueSize() < 20) {
//				waitSomeTime();
				Person person = createRandomPerson(counter);
				person.start();
				System.out.println("counter: " + counter);
				counter++;
			}
		}
	}

	public static void main(String[] args) {
		try {
			Autorai autorai = new Autorai();
			autorai.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Person createRandomPerson(int counter) {
		int chance = new Random().nextInt(100);
		if (chance > 50) {
			return new Buyer("b" + counter, showRoom);
		}
		return new Viewer("v" + counter, showRoom);
	}

	private void waitSomeTime() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
